<?php
namespace app\admin\controller;
use app\service\repairService;
use printing\printDemo;
use think\facade\View;

class Proposal extends Common{
    public function proposalList(){
        //根据当前登录账号查询物业公司
        $com = $this->pidTokenId();
        $whe['community_id'] = array('in',$com['community_id']);
        $page = request()->has('page') ? request()->get('page') : 1;
        $proposallist = (new repairService())->proposalList($whe,$page);
        $number = count((new repairService())->proposalNumber($whe));
        View::assign('number',$number);
        
        View::assign('list',$proposallist);
        View::assign('proposallist',$proposallist);
        return View::fetch('proposal_list');
    }
    public function proposalAdd(){
        $community_id = request()->get('community_id');
        $whe['id'] = array('in',$community_id);
        $communitylist = (new repairService())->communityList($whe);
        View::assign('communitylist',$communitylist);
        if(request()->isPost()){
            //获取表单数据
            $data = request()->post();
            $data['time'] = date('Y-m-d H:i:s',time());
            //插入数据到数据库
            $result = (new repairService())->proposalAdd($data);
            if(!empty($result)){
                //根据ID查询报修信息
                $proposalidlist = (new repairService())->proposalIdList($result);
                //根据小区ID查询打印机信息
                $communityidinfo = (new repairService())->communityIdInfo($proposalidlist['community_id']);
                $machineCode = $communityidinfo['machine_code'];
                $community = $communityidinfo['name'];
                $people = $proposalidlist['people'];
                $phone = $proposalidlist['phone'];
                //根据报修类型ID查询报修
                if($proposalidlist['type'] == 1){
                    $type = '投诉';
                }elseif($proposalidlist['type'] == 2){
                    $type = '建议';
                }elseif($proposalidlist['type'] == 3){
                    $type = '表扬';
                }else{
                    $type = '';
                }
                $contents = $proposalidlist['content'];
                $time = $proposalidlist['time'];
                $printing = (new printDemo())->proposalPrinting($machineCode,$community,$people,$phone,$type,$contents,$time);
                $this->success('提交建议成功',url('proposalAdd'));
            }else{
                $this->error('提交建议失败',url('proposalAdd'));
            }             
        }
        return View::fetch('proposal_add');
    }
}